<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="相对方" prop="counterpartId">
        <el-select v-model="queryParams.counterpartId" style="width: 300px;" @change="getContractChange" placeholder="请选择合同对象">
          <el-option
            v-for="item in counterpartList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合同" prop="contractId">
        <el-select v-model="queryParams.contractId" style="width: 300px;"  placeholder="请选择合同">
          <el-option
            v-for="item in contractList"
            :key="item.id"
            :label="item.contractName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table show-overflow-tooltip v-loading="loading" border :data="accountsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="相对方" align="center" prop="contractName"width="100" />
      <el-table-column label="合同" align="center" prop="contractName" width="150"/>
      <el-table-column label="项目" align="center" prop="projectName" width="150"/>
      <el-table-column label="回款金额" align="center" prop="amount" />
      <el-table-column label="回款日期" align="center" prop="amountDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.amountDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" width="150"/>
      <el-table-column label="开户银行" align="center" prop="bank" width="150"/>
      <el-table-column label="开户名称" align="center" prop="bankAccountName" width="150"/>
      <el-table-column label="银行账户" align="center" prop="bankNumber" width="150"/>
      <el-table-column label="纳税人识别号" width="150" align="center" prop="systemTin"/>
      <el-table-column label="地址" align="center" prop="address" width="150"/>
      <el-table-column label="电话" align="center" prop="tel" width="150" />
      <el-table-column label="操作" align="center"  fixed="right" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账款管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="15">
        <el-col :span="12">
            <el-form-item label-width="120px" label="相对方" prop="counterpartId">
              <el-select v-model="form.counterpartId" style="width: 300px;" @change="getContractChange" placeholder="请选择合同对象">
                <el-option
                  v-for="item in counterpartList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="合同" prop="contractId">
              <el-select v-model="form.contractId" style="width: 300px;"  placeholder="请选择合同">
                <el-option
                  v-for="item in contractList"
                  :key="item.id"
                  :label="item.contractName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="项目" prop="projectId">
              <el-select v-model="form.projectId" style="width: 300px;"  placeholder="请选择合同">
                <el-option
                  v-for="item in projectList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label-width="120px" label="回款金额" prop="amount">
              <el-input style="width: 300px;" v-model="form.amount" placeholder="请输入回款金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label-width="120px" label="回款日期" prop="amountDate">
              <el-date-picker clearable
                              v-model="form.amountDate"
                              type="date"
                              style="width: 300px;"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择回款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="备注" prop="remarks">
              <el-input style="width: 300px;" v-model="form.remarks" type="text" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="开户银行" prop="bank">
              <el-input style="width: 300px;" v-model="form.bank" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="开户名称" prop="bankAccountName">
              <el-input style="width: 300px;" v-model="form.bankAccountName" placeholder="请输入开户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="银行账户" prop="bankNumber">
              <el-input style="width: 300px;" v-model="form.bankNumber" placeholder="请输入银行账户" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="纳税人识别号" prop="systemTin">
              <el-input style="width: 300px;" v-model="form.systemTin" placeholder="请输入纳税人识别号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="地址" prop="address">
              <el-input style="width: 300px;" v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label-width="120px" label="电话" prop="tel">
              <el-input style="width: 300px;" v-model="form.tel" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="部门" prop="deptId">
              <treeselect  style="width: 300px;" v-model="form.deptId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccounts, getAccounts, delAccounts, addAccounts, updateAccounts } from "@/api/contract/accounts";
import { listAllProject} from "@/api/contract/project";
import {listDept, listDeptExcludeChild} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listCounterpartAll} from "@/api/contract/counterpart";
import {listManageAll} from "@/api/contract/manage";

export default {
  name: "Accounts",
  components: { Treeselect },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账款管理表格数据
      accountsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        counterpartId: null,
        contractId: null,
        amount: null,
        amountDate: null,
        remarks: null,
        bank: null,
        bankAccountName: null,
        bankNumber: null,
        systemTin: null,
        address: null,
        tel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deptOptions:[],
      counterpartList:[],
      contractList:[],
      projectList:[],

    };
  },
  created() {
    this.getList();
  },
  methods: {
    getContractChange(){
      this.contractList = []
      this.form.contractId =  null;
      var params = {contractCounterpart:this.form.counterpartId}
      listManageAll(params).then( res =>{
        this.contractList = res.data;
      })
    },
    /** 查询账款管理列表 */
    getList() {
      this.loading = true;
      listAccounts(this.queryParams).then(response => {
        this.accountsList = response.rows;
        this.total = response.total;
        this.loading = false;
        listCounterpartAll({}).then( res =>{
          this.counterpartList = res.data;
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.contractList = [];
      this.form = {
        id: null,
        deptId: null,
        counterpartId: null,
        contractId: null,
        amount: null,
        amountDate: null,
        remarks: null,
        bank: null,
        bankAccountName: null,
        bankNumber: null,
        systemTin: null,
        address: null,
        tel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "deptId");
      });
      listAllProject().then(response => {
        this.projectList = response.data
      });
      this.reset();
      this.open = true;
      this.title = "添加账款管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccounts(id).then(response => {
        listDept().then(response => {
          this.deptOptions = this.handleTree(response.data, "deptId");
        });
        listCounterpartAll({}).then( res =>{
          this.counterpartList = res.data;
        })
        listAllProject().then(response => {
          this.projectList = response.data
        });
        var params = {contractCounterpart:response.data.counterpartId}
        listManageAll(params).then( res =>{
          this.contractList = res.data;
        })

        this.form = response.data;
        this.open = true;
        this.title = "修改账款管理";
      });

    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccounts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccounts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除账款管理编号为"' + ids + '"的数据项？').then(function() {
        return delAccounts(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/accounts/export', {
        ...this.queryParams
      }, `accounts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
